import random
import time

print('欢迎来到我的小超市')


balance = 0
cart = []
totle = 0

couopon = [
    {'type':'空调','discount':0.8},
    {'type':'冰箱','discount':0.95},
    {'type':'笔记本','discount':0.65},
    {'type':'羽绒服','discount':0.8},
    {'type':'洗头膏','discount':0.7},
    {'type':'矿泉水','discount':0.9},
    {'type':'帽子','discount':0.9},
    {'type':'苹果手机','discount':0.9},
    {'type':'裤子','discount':0.9},
    {'type':'上衣','discount':0.9},
]

product = {
    '卫生纸': 1,
    '矿泉水': 2,
    '洗头膏': 28,
    '上衣': 89,
    '裤子': 69,
    '空调': 1399,
    '冰箱': 599,
    '羽绒服': 999,
    '帽子': 200,
    '苹果手机': 9999,
    '平板': 2300,
    '笔记本': 4500
}

#抽取优惠券
select_coupon = random.choice(couopon)
print(f"您抽到的优惠券为{select_coupon['type']},这件商品的折扣为{select_coupon['discount']}")


#系统开启循环
while True:
#显示商品
    print('本超市正在进货，请选择现有商品')
    for item, price in product.items():
        print(f'{item}: {price}元')
    print('输入c查看购物车，输入i充值，输入q退出并结账')
    select_product = input('请输入您想购买的商品：').strip()

#充值金额i
    if select_product == 'i' or select_product == 'I':
        while True:
            money = int(input('您想充值多少金额:'))
            balance += money
            print('按下f退出充值系统，按下i继续充值')
            en = input('请输入:')
            if en == 'f':
                break
            else:
                continue
        print(f'您当前的余额为{balance}')
        continue

#购物车内的内容c
    if select_product == 'c' or select_product == 'C':
        while True:
            if not cart:
                print('您的购物车为空')
            else:
                for item, price in cart:
                    if item == select_coupon['type']:
                        print(f'{item}：优惠后为{price}元')
                    else:
                        print(f'{item}：{price}元')
                print(f'共消费：{totle}元')
            select_order=input("输入d可进入删除指定订单，输入q退出致主页:")
            if select_order == 'd' or select_order == 'd':
                del_order=input('请输入你想删除订单的名字:')
                for item,price in cart:
                    if del_order == item:
                        cart.remove((item,price))
                        totle = sum([price for item in cart])
                        print(f'商品{item}已被删除，当前购物车为{cart},总消费为{totle}')
                        break
                break
            else:
                break

#购物小条q
    if select_product == 'q' or select_product == 'Q':
        print('感谢下次光临小店')
        print('\n------购物小条-----')
        current_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
        print(f"购物时间为{current_time}")
        print(f"购物数量为{len(cart)}")

        #计算总价
        for item, price in cart:
            if item == select_coupon['type']:
                discount_price = price * select_coupon['discount']
                print(f'商品{item}打折为{discount_price}元')
                continue
            print(f'{item}：{price}元')
        print(f'优惠后总消费{totle}元')
        balance -= totle
        print(f'您的卡的余额为{balance}元')
        print('------------------')
        break
#没有该商品
    if select_product not in product:
        print('抱歉，没有该商品，请继续输入')
        continue

    product_price = product[select_product]
#判断余额是否足够加入购物车
    if select_product == select_coupon['type']:
        product_price = round(product_price * select_coupon['discount'], 2)
        if balance >= (totle + product_price):
            #检查是否有优惠
                print(f'您已成功将{select_product}加入购物车')
                cart.append((select_product, product_price))
                totle += product_price
                print(f'您还有{balance - totle}预算购买其他商品')
        else:
            print(f'你的预算不足以购买{select_product}，请充值')
    else:
        if balance >= (totle + product_price):
            #检查是否有优惠
                print(f'您已成功将{select_product}加入购物车')
                cart.append((select_product, product_price))
                totle += product_price
                print(f'您还有{balance - totle}预算购买其他商品')
        else:
            print(f'你的预算不足以购买{select_product}，请充值')